package com.mia.craftstudio;

import com.mia.craftstudio.libgdx.Quaternion;
import com.mia.craftstudio.libgdx.Vector3;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class CSModelAnim {
   private final byte asset_type;
   private final short format_version;
   private final short animation_duration;
   private final boolean hold_last_keyframe;
   private final short node_count;
   private final HashMap nodes = new HashMap();
   private HashSet affectedNodes = null;

   public CSModelAnim(ByteBuffer buffer) throws CSExceptions.TypeMismatchException, CSExceptions.UnsupportedVersionException {
      this.asset_type = buffer.get();
      if (this.asset_type != 6) {
         throw new CSExceptions.TypeMismatchException((byte)6, this.asset_type);
      } else {
         this.format_version = buffer.getShort();
         if (this.format_version != 3) {
            throw new CSExceptions.UnsupportedVersionException(this.format_version);
         } else {
            this.animation_duration = buffer.getShort();
            this.hold_last_keyframe = buffer.get() != 0;
            this.node_count = buffer.getShort();

            for(int i = 0; i < this.node_count; ++i) {
               CSModelAnim.ModelNodeAnimation node = new CSModelAnim.ModelNodeAnimation(buffer);
               this.nodes.put(node.getName(), node);
            }

         }
      }
   }

   public short getNode_count() {
      return this.node_count;
   }

   public HashMap getNodes() {
      return this.nodes;
   }

   public CSModelAnim.ModelNodeAnimation getNodeAffectingModel(String modelName) {
      return (CSModelAnim.ModelNodeAnimation)this.nodes.get(modelName);
   }

   public Set getModelNamesAffected() {
      return this.nodes.keySet();
   }

   public short getAnimationDuration() {
      return this.animation_duration;
   }

   public boolean isHoldingLastKeyframe() {
      return this.hold_last_keyframe;
   }

   public String toString() {
      StringBuilder ret = new StringBuilder(String.format("-=-=-=-=-\nCSModelAnimation: Duration[%d] HoldLastKeyframe[%b] NodeCount[%d] :\n", this.animation_duration, this.hold_last_keyframe, this.node_count));
      Iterator var2 = this.nodes.entrySet().iterator();

      while(var2.hasNext()) {
         Entry entry = (Entry)var2.next();
         ret.append(entry.getValue() + "\n");
      }

      return ret.toString();
   }

   public static enum KeyFrameType {
      Position,
      Orientation,
      BlockSize,
      PivotOffset,
      Scale;
   }

   public static class ModelNodeKeyFrame {
      private final CSModelAnim.KeyFrameType type;
      private final short key_time_index;
      private final byte interpolation_mode;
      private final Vector3 vector;
      private final Quaternion quaternion;
      public final FloatBuffer quatbuffer;

      public ModelNodeKeyFrame(CSModelAnim.KeyFrameType frameType, ByteBuffer buffer) {
         this.type = frameType;
         this.key_time_index = buffer.getShort();
         this.interpolation_mode = buffer.get();
         switch(frameType) {
         case Orientation:
            this.vector = null;
            this.quaternion = new Quaternion(buffer);
            float[] quatmatrix = new float[16];
            Quaternion var10000 = this.quaternion;
            var10000.y *= -1.0F;
            var10000 = this.quaternion;
            var10000.z *= -1.0F;
            this.quaternion.toMatrix(quatmatrix);
            this.quatbuffer = FloatBuffer.allocate(16);
            this.quatbuffer.put(quatmatrix);
            this.quatbuffer.flip();
            break;
         case BlockSize:
            this.vector = new Vector3((float)buffer.getInt(), (float)buffer.getInt(), (float)buffer.getInt());
            this.quaternion = null;
            this.quatbuffer = null;
            break;
         default:
            this.vector = new Vector3(buffer);
            this.quaternion = null;
            this.quatbuffer = null;
         }

      }

      public byte getInterpolationMode() {
         return this.interpolation_mode;
      }

      public short getKeyTimeIndex() {
         return this.key_time_index;
      }

      public CSModelAnim.KeyFrameType getType() {
         return this.type;
      }

      public Vector3 getVector() {
         return this.vector;
      }

      public Quaternion getQuaternion() {
         return this.quaternion.cpy();
      }

      public String toString() {
         return String.format("Keyframe: Type[%s] Index[%s] Mode[%s] Vector[%s] Quaternion[%s]", this.type.name(), this.key_time_index, this.interpolation_mode, this.vector, this.quaternion);
      }
   }

   public static class ModelNodeAnimation {
      private final String name;
      private final short position_keyframe_count;
      private final ArrayList position_keyframes = new ArrayList();
      private final short orientation_keyframe_count;
      private final ArrayList orientation_keyframes = new ArrayList();
      private final short block_size_keyframe_count;
      private final ArrayList block_size_keyframes = new ArrayList();
      private final short pivot_offset_keyframe_count;
      private final ArrayList pivot_offset_keyframes = new ArrayList();
      private final short scale_keyframe_count;
      private final ArrayList scale_keyframes = new ArrayList();

      public String getName() {
         return this.name;
      }

      public ArrayList getOrientationKeyframes() {
         return this.orientation_keyframes;
      }

      public String toString() {
         StringBuilder ret = new StringBuilder(String.format("ModelNodeAnimation for ModelNode[%s]:\n", this.name));
         Iterator var2;
         CSModelAnim.ModelNodeKeyFrame keyframe;
         if (this.position_keyframe_count > 0) {
            ret.append(String.format("\tPosition Keyframe count: %s\n", this.position_keyframe_count));
            var2 = this.position_keyframes.iterator();

            while(var2.hasNext()) {
               keyframe = (CSModelAnim.ModelNodeKeyFrame)var2.next();
               ret.append("\t\t" + keyframe + "\n");
            }
         }

         if (this.orientation_keyframe_count > 0) {
            ret.append(String.format("\tOrientation Keyframe count: %s\n", this.orientation_keyframe_count));
            var2 = this.orientation_keyframes.iterator();

            while(var2.hasNext()) {
               keyframe = (CSModelAnim.ModelNodeKeyFrame)var2.next();
               ret.append("\t\t" + keyframe + "\n");
            }
         }

         if (this.block_size_keyframe_count > 0) {
            ret.append(String.format("\tBlock Size Keyframe count: %s\n", this.block_size_keyframe_count));
            var2 = this.block_size_keyframes.iterator();

            while(var2.hasNext()) {
               keyframe = (CSModelAnim.ModelNodeKeyFrame)var2.next();
               ret.append("\t\t" + keyframe + "\n");
            }
         }

         if (this.pivot_offset_keyframe_count > 0) {
            ret.append(String.format("\tPivot Offset Keyframe count: %s\n", this.pivot_offset_keyframe_count));
            var2 = this.pivot_offset_keyframes.iterator();

            while(var2.hasNext()) {
               keyframe = (CSModelAnim.ModelNodeKeyFrame)var2.next();
               ret.append("\t\t" + keyframe + "\n");
            }
         }

         if (this.scale_keyframe_count > 0) {
            ret.append(String.format("\tScale Keyframe count: %s\n", this.scale_keyframe_count));
            var2 = this.scale_keyframes.iterator();

            while(var2.hasNext()) {
               keyframe = (CSModelAnim.ModelNodeKeyFrame)var2.next();
               ret.append("\t\t" + keyframe + "\n");
            }
         }

         return ret.toString();
      }

      public ModelNodeAnimation(ByteBuffer buffer) {
         byte[] tmpName = new byte[buffer.get()];
         buffer.get(tmpName);
         this.name = new String(tmpName);
         this.position_keyframe_count = buffer.getShort();

         int i;
         for(i = 0; i < this.position_keyframe_count; ++i) {
            this.position_keyframes.add(new CSModelAnim.ModelNodeKeyFrame(CSModelAnim.KeyFrameType.Position, buffer));
         }

         this.orientation_keyframe_count = buffer.getShort();

         for(i = 0; i < this.orientation_keyframe_count; ++i) {
            this.orientation_keyframes.add(new CSModelAnim.ModelNodeKeyFrame(CSModelAnim.KeyFrameType.Orientation, buffer));
         }

         this.block_size_keyframe_count = buffer.getShort();

         for(i = 0; i < this.block_size_keyframe_count; ++i) {
            this.block_size_keyframes.add(new CSModelAnim.ModelNodeKeyFrame(CSModelAnim.KeyFrameType.BlockSize, buffer));
         }

         this.pivot_offset_keyframe_count = buffer.getShort();

         for(i = 0; i < this.pivot_offset_keyframe_count; ++i) {
            this.pivot_offset_keyframes.add(new CSModelAnim.ModelNodeKeyFrame(CSModelAnim.KeyFrameType.PivotOffset, buffer));
         }

         this.scale_keyframe_count = buffer.getShort();

         for(i = 0; i < this.scale_keyframe_count; ++i) {
            this.scale_keyframes.add(new CSModelAnim.ModelNodeKeyFrame(CSModelAnim.KeyFrameType.Scale, buffer));
         }

      }
   }
}
